﻿using MangoMes.Commont.DapperHelps;
using MangoMes.Commont.Domain;
using MangoMes.Commont.Domain.Department;
using MangoMes.Commont.DTOs;
using MangoMes.Commont.Infrastructures.Interfaces;
using MediatR;
using System.Diagnostics.Metrics;

namespace MangoMes.Write.API.Applications.Departmentes
{
    public class DepartmentslistHandler : IRequestHandler<DepartmentslistCommond, PageDTO<Departments>>
    {
        private readonly IBateRepository<Departments> depart;
        public DepartmentslistHandler(IBateRepository<Departments> depart)
        {
            this.depart = depart;
        }

        public async Task<PageDTO<Departments>> Handle(DepartmentslistCommond request, CancellationToken cancellationToken)
        {
            PageDTO<Departments> departmentns =new PageDTO<Departments>();
            DapperHelper helper = new DapperHelper();
            Departments ddep = new Departments();
            var pageNum = (request.pageIndex - 1) * request.pageSize;
            string conn =helper.MangoMesWrite ;
            string str = "select * from department order by departmentId limit "+ request.pageSize + " offset " + pageNum
            + "";
            string stra = "select * from department";
            var counts= depart.GetAll(stra, ddep, conn).ToList().Count();
            departmentns.TotalCount = counts;
            departmentns.PageCount = (int)Math.Ceiling(counts * 1.0 / request.pageSize);

            departmentns.Data = depart.GetAll(str, ddep, conn).ToList();
            return departmentns;

        }
    }
}
    